package com.sx.pipeline.ui.login.frag

import android.app.Application
import android.text.TextUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.config.LoginType
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.LoginDTO
import com.sx.pipeline.repos.http.res.LoginVO

/**
 * @Author: YY
 * @Date: 2022/4/6
 * @Description: TODO
 */
class LoginVM(app: Application) : CYBaseViewModel(app) {


    /**
     * 登录
     */
    fun login(loginType: Int, phone: String, code: String, password: String,
              appCode: String = "", isBind: Boolean = false) {

        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入手机号")
            return
        }

        // 验证码登录
        if (loginType == LoginType.VERIFY_CODE_LOGIN.code && TextUtils.isEmpty(code)) {
            ToastUtils.showShort("请输入验证码")
            return
        }

        // 账号登录
        if (loginType == LoginType.ACCOUNT_LOGIN.code && TextUtils.isEmpty(password)) {
            ToastUtils.showShort("请输入密码")
            return
        }

        val dto = LoginDTO(loginType.toString(), phone,
            code, password, appCode, isBind)

        addSubscribe(mModel.login(dto)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<LoginVO, BaseViewModel<*>>
                (this, true) {

                override fun onResult(result: LoginVO) {
                    mModel.setToken(result.token!!)
                    mModel.saveIdentityType(result.userIdentity)
                    getUserInfo()
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort("$msg")

                }
            })
        )
    }

//    /**
//     * 获取用户信息
//     */
//    fun getUserInfo() {
//        addSubscribe(mModel.getUserInfo()
//            .compose(RxThreadHelper.rxSchedulerHelper())
//            .subscribeWith(object : CommonObserver<UserInfoVO, BaseViewModel<*>>
//                (this, true) {
//                override fun onResult(result: UserInfoVO) {
//                    mModel.saveUserData(result)
//                    RouteManager.Main.intentToMain(getApplication(),
//                        object : LoginNavCallbackImpl() {
//                            override fun onArrival(postcard: Postcard?) {
//                                finish()
//                            }
//                        })
//                }
//
//                override fun onFailed(code: Int, msg: String?) {
//                    super.onFailed(code, msg)
//                    ToastUtils.showShort(msg)
//                }
//
//                override fun onError(e: Throwable) {
//                    super.onError(e)
//                    ToastUtils.showShort(e.message.toString())
//                }
//            })
//        )
//    }

}